<script setup>
import { ref } from 'vue'
import {selectLeaveClassService,
    insertExamListClassService,
    updateExamListClassService,
    delExamListService,
    pushEmailService
} from '@/api/ExamManager.js'

const total = ref(0)
const params = ref({
    courseName:'',
    examName:'',
  pageNum:1,
  pageSize:5
})

const xsExamList = ref([])
const xsExamVoRef = ref()
const xsExamVo = ref({
      id:'',
      examName:'',
      courseId:'',
      examLocation:'',
      examDesc:'',
      examTime:null,
      examTimeStr:'',
      courseName:'',
      status:'',
      statusStr:'',
})
const selectLeaveClass = async () => {
    const res = await selectLeaveClassService(params.value)
    xsExamList.value = res.data.data
    total.value = res.data.total
}
selectLeaveClass()
const handleClick = () => {
  console.log('click')
}
const rules = {
    courseId:[{required:true,message:'请输入课程编号',trigger:'blur'}],
    examName:[{required:true,message:'请输入考试名称',trigger:'blur'}],
    examTime:[{required:true,message:'请输入考试时间',trigger:'blur'}],
    examLocation:[{required:true,message:'请输入考试地点',trigger:'blur'}],
    status:[{required:true,message:'请输入考试状态',trigger:'blur'}],
}

const showDrawer =ref(false)
const editInsertBtn = () => {
    showDrawer.value = true
    xsExamVo.value={}
}
const editRow = async (row) =>{
    showDrawer.value = true
    xsExamVo.value = row
}
const deleteRow = async (row) =>{
    await delExamListService(row)
}

const confirmClick = async()=>{
    await xsExamVoRef.value.validate()
    if(xsExamVo.value.id){
        await updateExamListClassService(xsExamVo.value)
    }else{
        await insertExamListClassService(xsExamVo.value)
    }

    ElMessage.success('成功！')
    xsExamVo.value={}
    showDrawer.value =  false
    selectLeaveClass()
}
const search = () =>{
    selectLeaveClass()
}

const pushRef = ref({
    examId:''
})
const pushRow = async (row) => {
    pushRef.value.examId = row.id
    await pushEmailService(pushRef.value)
    ElMessage.success('邮件已通知')
}

</script>

<template>
  <div>
    <div class="cus-header">
        <el-card>
            <div style="display: flex;justify-content: space-between;">
                <div class="search">
                    <div>
                        课程：
                        <el-input v-model="params.courseName" style="width: 240px" placeholder="请输入课程名称" />
                    </div>
                    <div style="margin-left: 20px;">
                        考试：
                        <el-input v-model="params.examName" style="width: 240px" placeholder="请输入考试名称" />
                    </div>
                    <div style="margin-left: 20px;">
                        <el-button @click="search" style="width: 80px;" type="primary">搜索</el-button>
                    </div>
                </div>
                <div class="insert">
                        <el-button style="width: 80px;" type="primary" @click="editInsertBtn">新增</el-button>
                </div>
            </div>
            
            
        </el-card>
    </div>
    <div class="cus-body">
        <el-card style="min-height: 650px;">
            <el-table :data="xsExamList" style="width: 100%">
                <el-table-column fixed prop="id" label="考试编号" width="150" />
                <el-table-column prop="examName" label="考试名称" width="120" />
                <el-table-column prop="examLocation" label="考试地点" width="120" />
                <el-table-column prop="examDesc" label="考试描述" width="120" />
                <el-table-column prop="examTimeStr" label="考试时间" width="120" />
                <el-table-column prop="courseName" label="课程名称" width="120" />
                <el-table-column prop="statusStr" label="状态" width="600" />
                <el-table-column fixed="right" label="操作" min-width="200">
                    <template #default="{row}">
                        <el-button link type="primary" size="small" @click="pushRow(row)">
                            通知
                        </el-button>
                        <el-button link type="primary" size="small" @click="editRow(row)">
                            编辑
                        </el-button>
                        <el-button link type="primary" size="small" @click="deleteRow(row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
  </div>

  <!-- 新增或编辑 -->
  <el-drawer v-model="showDrawer" >
        <template #header>
            <h4>操作</h4>
        </template>
        <template #default>
            <div>
                <el-form
                :model="xsExamVo" 
                ref="xsExamVoRef"
                :rules="rules"
                label-width="auto">
                    <el-form-item label="课程编号" :label-position="left" prop="courseId">
                        <el-input v-model="xsExamVo.courseId" />
                    </el-form-item>
                    <el-form-item label="考试名称" :label-position="left" prop="examName">
                        <el-input v-model="xsExamVo.examName" />
                    </el-form-item>
                    <el-form-item label="考试地点" :label-position="left" prop="examLocation">
                        <el-input v-model="xsExamVo.examLocation" />
                    </el-form-item>
                    <el-form-item label="状态" :label-position="left" prop="status">
                        <el-radio v-model="xsExamVo.status" label="0">待发布</el-radio>
                        <el-radio v-if="xsExamVo.id" v-model="xsExamVo.status" label="1">发布</el-radio>
                    </el-form-item>
                    <el-form-item label="考试时间" :label-position="left" prop="examTime">
                        <el-date-picker
                        v-model="xsExamVo.examTime"
                        type="datetime"
                        placeholder="选择日期时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="考试描述" :label-position="left" prop="examDesc">
                        <el-input v-model="xsExamVo.examDesc" />
                    </el-form-item>
                </el-form>
            </div>
        </template>
        <template #footer>
        <div style="flex: auto">
            <el-button @click="showDrawer = false">取消</el-button>
            <el-button type="primary" @click="confirmClick">提交</el-button>
        </div>
        </template> 
    </el-drawer>
</template>


<style lang="scss" scoped>
    .flex-row{
        display: flex;
        flex-direction: row;
    }
    .cus-header{
        .search{
            display: flex;
            flex-direction: row;
        }
    }
    .cus-body{
        margin-top: 15px;
    }
</style>